package servlet;

import com.alibaba.fastjson.JSON;
import model.Address;
import model.User;
import service.AddressService;
import service.impl.AddressServiceImpl;
import utils.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Description:
 * @Author one world
 * @Date 2020/9/26 0026 18:02
 */
@WebServlet("/address")
public class AddressServlet extends BaseServlet {
    AddressService service = new AddressServiceImpl();
    public void findAddressByOrderNo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String order_no = request.getParameter("order_no");
        if(order_no==null||order_no.equals("")){
            return;
        }
        Address address = service.findByOrderNo(order_no);
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(address));
    }
    public void update(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int user_id = Integer.parseInt(request.getParameter("user_id"));
        String name = request.getParameter("name");
        String shipping = request.getParameter("shipping");
        String phone = request.getParameter("phone");
        Address address = new Address(id,user_id,shipping,name,phone);
        PrintWriter out = response.getWriter();;
        if(service.update(address)){
            out.write(JSON.toJSONString("修改成功"));
        }else {
            out.write(JSON.toJSONString("修改失败"));
        }
        out.close();
    }
    public void addAddress(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String shipping = request.getParameter("shipping");
        User user = (User)request.getSession().getAttribute("user");
        int id = user.getId();
        if(service.add(new Address(id,shipping,name,phone))){
            response.getWriter().write("SUCCESS");
        }
    }
}
